//
//  UserCenterApiTool.swift
//  MT
//
//  Created by ooops on 2020/1/2.
//  Copyright © 2020 张强. All rights reserved.
//

import Foundation

import ObjectMapper
import MTCategory
import Result


class UserCenterApiTool: NSObject {
    
    class func notify(provider: MTProvider<UserCenterApi>,
                      requestParam: [String : Any],
                      complete: @escaping (MTUserNotifyModel?) -> Swift.Void,
                      failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.notify(requestParam)).subscribe(onNext: { (result:Result<MTUserNotifyModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let listModel):
                if let lists: [MTUserNotifySubModel] = listModel.result {
                    for (i, v) in lists.enumerated() {
                        if let msgId = v.msgId, let status: Int = v.status, let s: MTGiftInfoStatus = MTGiftInfoStatus(rawValue: status) {
                            let giftInfo: MTGiftInfo = MTGiftInfo.create(msgId: "\(msgId)", status: s)
                            MTGiftInfoDB.shareInstance.insertData(giftInfo)
                        }
                    }
                }
                complete(listModel)
                break
            case .failure(_):
                break
            }
            
        }).disposed(by: globalDisposedBag)
    }
    
    
    /// 搜索用户接口 - 添加好友用
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func search(provider: MTProvider<UserCenterApi>,
                      requestParam: [String : Any],
                      complete: @escaping (MTSearchUserModel?) -> Swift.Void,
                      failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.search(requestParam)).subscribe(onNext: { (result:Result<MTSearchUserModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let listModel):
                complete(listModel)
                break
            case .failure(let error):
                failure(error)
                break
            }
            
        }).disposed(by: globalDisposedBag)
    }
}
